package com.zhuss.product.processor;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.product.DeleteProductBo;
import com.zhuss.bean.vo.product.product.DetailProductVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.product.db.dao.ext.ProductDao;
import com.zhuss.product.db.entity.Product;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.ShopException;
import com.zhuss.util.JsonUtil;

/**
 * @description: 商品删除
 * @author: zhushanshuo
 * @create: Wed Mar 24 22:26:45 CST 2021
 **/
@Component("productDelete")
public class ProductDelete extends AbstractProcessor<Object, DeleteProductBo> implements Processor<Object, DeleteProductBo> {

    @Autowired
    private ProductDao productDao;
    
    @Autowired
	@Qualifier("productRead")
	private ProductRead productRead;
    
    
    /**
     * 校验
     */
	@Override
    public void check(DeleteProductBo bo) throws ShopException {
		// TODO 删除 商品 校验， 请补充
    }
    
    /**
     * 操作方法
     */
    @Override
    public Object process(DeleteProductBo param) throws ShopException {
    	Long productId = param.getProductId();
    	String name = ((DetailProductVo) productRead.detail(productId)).getName();
    	
    	Product record = new Product();
    	record.setProductId(productId);
    	record.setDeleted(param.getDeleted());
    	
    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	productDao.delete(record);
    	param.setName(name);
        return null;
    }
    
    /**
     * 操作日志
     */
	@Override
	public Object log(DeleteProductBo param) throws ShopException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品模块.key());
    	record.setAction(LogActionEnum.商品删除.key());
    	record.setTargetId(param.getProductId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
